//
//  JokerCellTableViewCell.m
//  FiveWork
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 Project. All rights reserved.
//

#import "JokerCellTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "JokerModel.h"
#define ScreenBounds [UIScreen mainScreen].bounds

@interface JokerCellTableViewCell ()
@property(nonatomic,strong)UIImageView *backImageView;
@property(nonatomic,strong)UIImageView *topImageView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *sourceLabel;
@property(nonatomic,strong)UIImageView *upImageView;
@property(nonatomic,strong)UIImageView *downImageView;
@property(nonatomic,strong)UILabel *upLabel;
@property(nonatomic,strong)UILabel *downLabel;
@property(nonatomic,strong)UIButton *button;

@end

@implementation JokerCellTableViewCell

-(instancetype )initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self =[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupSubViews];
    }
    return self;
}
-(void)setupSubViews{
    self.backgroundView =[[UIImageView alloc] init];
    self.backImageView.image =[UIImage imageNamed:@"bg_share_large"];
    [self addSubview:self.backgroundView];
    
    self.topImageView =[[UIImageView alloc] init];
    [self addSubview:self.topImageView];
    [self.topImageView setBackgroundColor:[UIColor grayColor]];
    self.titleLabel =[[UILabel alloc] init];
    [self.titleLabel setFont:[UIFont systemFontOfSize:15]];
    
    self.titleLabel.numberOfLines =0;
    
    [self addSubview:self.titleLabel];
    
    self.sourceLabel =[[UILabel alloc] init];
    [self.sourceLabel setFont:[UIFont systemFontOfSize:12]];
    self.sourceLabel.textColor =[UIColor darkGrayColor];
    [self addSubview:self.sourceLabel];
//    self.upImageView.image = [UIImage imageNamed:@"zan"];
//    self.downImageView.image = [UIImage imageNamed:@"buzan"];
//
//    self.upLabel = [[UILabel alloc]init];
//    [self.upLabel setFont:[UIFont systemFontOfSize:11]];
//    [self addSubview:self.upLabel];
//    self.downLabel = [[UILabel alloc]init];
//    [self.downLabel setFont:[UIFont systemFontOfSize:11]];
//    [self addSubview:self.downLabel];
//    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
//    [self addSubview:self.button];
//    [self.button setBackgroundImage:[UIImage imageNamed:@"shoucang"] forState:UIControlStateNormal];
}

-(void)setModel:(JokerModel *)model{
    if (_model !=model) {
        _model =nil;
        _model =model;
        [self layoutModel];
    }
}

-(void)layoutModel{
    [self setTopImageViewHeightWithModel:self.model];
    
    [self.topImageView sd_setImageWithURL:[NSURL URLWithString:self.model.imgsrc]];
    
    self.titleLabel.text =self.model.title;
    self.sourceLabel.text =self.model.source;
    self.upLabel.text =[NSString stringWithFormat:@"%ld",self.model.upTimes];
    self.downLabel.text =[NSString stringWithFormat:@"%ld",self.model.downTimes];
    [self setTopImageViewHeightWithModel:self.model];
    
    CGFloat height =[[self class] heightOfTopImageviewWithPixel:self.model.pixel];
    [self.backgroundView setFrame:CGRectMake(5, 5, ScreenBounds.size.width -10, height +80)];
    
    

    
    
    
    
    
    
    
}


/**
 *  设置Views的frame
 */


-(void)setTopImageViewHeightWithModel:(JokerModel *)model{
    CGFloat topHeight =[[self class]heightOfTopImageviewWithPixel:model.pixel];
    self.topImageView.frame =CGRectMake(10, 10, ScreenBounds.size.width -20, topHeight);
    CGFloat titleHeight =self.topImageView.frame.size.height +5 +self.topImageView.frame.origin.x;
    self.titleLabel.frame =CGRectMake(10, titleHeight, ScreenBounds.size.width -20, 30);
    self.sourceLabel.frame =CGRectMake(10, titleHeight +35, 100, 20);
    self.upLabel.frame =CGRectMake(250, titleHeight+35, 50, 20);
    self.upImageView.frame =CGRectMake(230, titleHeight +35, 20, 20);
    self.downLabel.frame = CGRectMake(310,titleHeight + 35 ,50 ,20 );
    self.downImageView.frame = CGRectMake(290, titleHeight + 35, 20, 20);
    self.button.frame = CGRectMake(180, titleHeight + 35, 20, 20);
    

    
    
    
    
    
    
    
    
}


/**
 *  cell高度
 */
+(CGFloat)heightOfCellWithModel:(JokerModel*)model{
    
    CGFloat height =[self heightOfTopImageviewWithPixel:model.pixel];
    return height +80;
  
}

/**
 *  图片自适应高度
 */
+(CGFloat)heightOfTopImageviewWithPixel:(NSString *)pixel{
    NSArray *pixeArr =[pixel componentsSeparatedByString:@"*"];
    CGFloat pix =(ScreenBounds.size.width -20  )*1.0/[pixeArr[0]integerValue] *1.0;
    CGFloat height =pix *[pixeArr [1]integerValue];
    
    return height;
    
}













- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
